from django.shortcuts import render
from django.views.generic import View
from django.http import HttpResponse

from .models import CourseOrg, CityDict

from .forms import UserAskForm
from courses.models import Course

# 引入第三方分页
from pure_pagination import Paginator, EmptyPage, PageNotAnInteger

# Create your views here.


# 课程机构列表
class OrgView(View):

    # GET请求返回
    def get(self, request):

        # 全部课程机构
        all_orgs = CourseOrg.objects.all()
        org_nums = all_orgs.count()  # 一共有多少条数据

        # 全部城市
        all_cities = CityDict.objects.all()

        # 根据点击量找热门机构
        hot_orgs = all_orgs.order_by("click_num")[:3]

        # 取出筛选城市
        city_id = request.GET.get('city', "")
        if city_id:
            # 外键可直接通过ID筛选
            all_orgs = all_orgs.filter(city_id=int(city_id))

        # 获取类别筛选
        category = request.GET.get('ct', "")
        if category:
            # 外键可直接通过ID筛选
            all_orgs = all_orgs.filter(category=category)

        # 取出排序数据
        sort = request.GET.get('sort', "")
        if sort:
            if sort == "students":
                all_orgs = all_orgs.order_by("-students")
            elif sort == "courses":
                all_orgs = all_orgs.order_by("-course_nums")

        # 对课程机构进行分页
        try:
            page = request.GET.get('page', 1)
        except PageNotAnInteger:
            page = 1

        p = Paginator(all_orgs, request=request, per_page=3)

        orgs = p.page(page)

        return render(request, "org-list.html", {
            "all_orgs": orgs,
            "all_cities": all_cities,
            "org_nums": org_nums,
            "city_id": city_id,
            "category": category,
            "hot_orgs": hot_orgs,
        })


# 我要学习
class AddUserAskView(View):

    # post 请求返回
    def post(self, request):

        user_ask_form = UserAskForm(request.POST)

        # 判断数据是否合规
        if user_ask_form.is_valid():
            user_ask = user_ask_form.save(commit=True)  # ModelForm 可以直接保存，与普通的 form 不同
            return HttpResponse('{"status": "success"}', content_type='application/json')
        else:
            return HttpResponse('{"status": "fail", "msg": "添加出错"}', content_type='application/json')


# 机构首页
class OrgHomeView(View):

    # GET请求返回
    def get(self, request, org_id):
        # 根据 org_id 取课程
        course_org = CourseOrg.objects.get(id=int(org_id))
        # django 外键可以通过以下方法，反向取数据
        all_courses = course_org.course_set.all()[:3]  # 限制只取三条数据
        all_teachers = course_org.teacher_set.all()

        return render(request, 'org-detail-homepage.html', {
            "all_courses": all_courses,
            "all_teachers": all_teachers,
            "course_org": course_org
        })


# 机构课程
class OrgCourseView(View):

    # GET请求返回
    def get(self, request, org_id):
        # 根据 org_id 取课程
        course_org = CourseOrg.objects.get(id=int(org_id))
        # django 外键可以通过以下方法，反向取数据
        all_courses = course_org.course_set.all()[:3]  # 限制只取三条数据

        return render(request, 'org-detail-course.html', {
            "all_courses": all_courses,
            "course_org": course_org
        })
